<template>
  <el-dialog
    :title="!dataForm.id ? '新建' : '编辑'"
    :close-on-click-modal="false"
    append-to-body
    :visible.sync="visible"
    class="jsbos-dialog jsbos-dialog_center"
    lock-scroll
    width="800px"
  >
    <el-row :gutter="15" class="">
      <el-form
        ref="elForm"
        :model="dataForm"
        :rules="rules"
        size="small"
        label-width="135px"
        label-position="right"
      >
        <template v-if="!loading">
          <el-col :span="24">
            <el-form-item label="名称" prop="fullName">
              <el-input
                v-trim
                v-model="dataForm.fullName"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="编码" prop="enCode">
              <span slot="label">
                <el-tooltip content="建议输入CorpId" placement="top">
                  <i
                    class="icon-ym icon-ym-header-about"
                    style="font-size: 14px"
                  ></i>
                </el-tooltip>
                编码
              </span>
              <el-input
                v-trim
                v-model="dataForm.enCode"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="CorpId" prop="enterpriseId">
              <el-input
                v-trim
                v-model="dataForm.enterpriseId"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="CorpSecret" prop="appKey">
              <el-input
                v-trim
                v-model="dataForm.appKey"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="AgentId" prop="agentId">
              <el-input
                v-trim
                v-model="dataForm.agentId"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="AgentSecret" prop="bearer">
              <el-input
                v-trim
                v-model="dataForm.bearer"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="token" prop="appId">
              <el-input
                v-trim
                v-model="dataForm.appId"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="encodingAESKey" prop="appSecret">
              <el-input
                v-trim
                autocomplete="new-password"
                v-model="dataForm.appSecret"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
                show-password
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否互联" prop="programState">
              <el-checkbox v-model="dataForm.programState"></el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="应用地址" prop="address">
              <el-input
                v-trim
                v-model="dataForm.address"
                placeholder="请输入"
                clearable
                :style="{ width: '100%' }"
                true
                type="textarea"
                :rows="1"
                :autosize="{ minRows: 1, maxRows: 4 }"
              >
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="排序" prop="sortCode">
              <el-input-number
                v-model="dataForm.sortCode"
                placeholder="数字文本"
                :step="1"
                controls-position="right"
              >
              </el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="状态" prop="enabledMark">
              <el-switch
                v-model="dataForm.enabledMark"
                :active-value="1"
                :inactive-value="0"
              >
              </el-switch>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="说明" prop="description">
              <el-input
                v-model="dataForm.description"
                placeholder="请输入"
                :style="{ width: '100%' }"
                true
                type="textarea"
                :rows="1"
                :autosize="{ minRows: 1, maxRows: 4 }"
              >
              </el-input>
            </el-form-item>
          </el-col>
        </template>
      </el-form>
    </el-row>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="btnLoading" @click="visible = false">
        取 消</el-button
      >
      <el-button :loading="btnLoading" type="primary" @click="dataFormSubmit()">
        确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import { getInfo, Update, Create } from "@/views/message/api/mcAccountConfig";
export default {
  components: {},
  props: {
    category: {
      type: String,
      default: "5",
    },
  },
  data() {
    var validateNumber = (rule, value, callback) => {
      console.log("value:" + value);
      if (!/^[+]{0,1}(\d+)$|^[+]{0,1}(\d+\.\d+)$/.test(value)) {
        callback(new Error("请输入数字值"));
      } else {
        callback();
      }
    };

    return {
      visible: false,
      loading: false,
      btnLoading: false,
      dataForm: {
        category: "",
        fullName: "",
        enCode: "",
        enterpriseId: "",
        appKey: "",
        agentId: "",
        bearer: "",
        appId: "",
        appSecret: "",
        programState: false, //是否互联企业
        address: "",
        sortCode: 0,
        enabledMark: 1,
        description: "",
      },
      rules: {
        fullName: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        enCode: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
          {
            pattern: "^[A-Za-z0-9_]+$",
            message: "只能输入字母（大写和小写）、数字或下划线",
            trigger: "blur",
          },
        ],
        enterpriseId: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        appKey: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        agentId: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
          {
            required: true,
            validator: validateNumber,
            trigger: "blur",
          },
        ],
        bearer: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        appId: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
        appSecret: [
          {
            required: true,
            message: "请输入",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    clearData(data) {
      for (let key in data) {
        if (data[key] instanceof Array) {
          data[key] = [];
        } else if (data[key] instanceof Object) {
          this.clearData(data[key]);
        } else {
          data[key] = "";
        }
      }
    },
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["elForm"].resetFields();
        if (this.dataForm.id) {
          this.loading = true;
          getInfo(this.dataForm.id).then((res) => {
            this.dataInfo(res.data);
            this.loading = false;
          });
        } else {
          this.clearData(this.dataForm);
          this.dataForm.enabledMark = 1;
          this.dataForm.programState = false; //是否互联企业
          this.dataForm.category = this.category; // 企业微信
        }
      });
      this.$store.commit("generator/UPDATE_RELATION_DATA", {});
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["elForm"].validate((valid) => {
        if (valid) {
          var _data = this.dataList();
          this.btnLoading = true;
          const formMethod = this.dataForm.id ? Update : Create;
          formMethod(_data)
            .then((res) => {
              this.$message({
                message: res.msg,
                type: "success",
                duration: 1000,
                onClose: () => {
                  this.visible = false;
                  this.btnLoading = false;
                  this.$emit("refresh", true);
                },
              });
            })
            .catch(() => {
              this.btnLoading = false;
            });
        }
      });
    },
    dataList() {
      var _data = JSON.parse(JSON.stringify(this.dataForm));
      _data.enabledMark = parseInt(_data.enabledMark);
      return _data;
    },
    dataInfo(dataAll) {
      let _dataAll = dataAll;
      _dataAll.enabledMark = parseInt(_dataAll.enabledMark);
      _dataAll.programState = JSON.parse(_dataAll.programState);
      this.dataForm = _dataAll;
    },
  },
};
</script>
